﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WarOfDev.EF;
using WarOfDev.Web.ViewModels;

namespace WarOfDev.Web.Models.MappingToViewModel
{
    public static class PermissionMapping
    {

        public static void BaseToViewModel(PermissionViewModel vm, Permission permission)
        {
            vm.Id = permission.Id;
            vm.PermissionName = permission.PermissionName;
            vm.PermissionValue = permission.PermissionValue;
            vm.ModifiedBy = permission.ModifiedBy;
            vm.ModifiedOn = permission.ModifiedOn;
            vm.CreatedBy = permission.CreatedBy;
            vm.CreatedOn = permission.CreatedOn;

            vm.ModuleId = permission.ModuleId;
            vm.Module = permission.Module.ToViewModel();
        }

        public static PermissionViewModel ToViewModel(this Permission permission)
        {
            PermissionViewModel vm = new PermissionViewModel();
            BaseToViewModel(vm, permission);
            return vm;
        }

        public static PermissionViewModel ToViewModel(this Permission permission, string selectedPermissions)
        {
            PermissionViewModel vm = new PermissionViewModel();
            BaseToViewModel(vm, permission);

            if (!string.IsNullOrEmpty(selectedPermissions))
            {
                var selected = selectedPermissions.Split(',');
                vm.IsSelected = selected.Contains(permission.PermissionValue.ToString());
            }

            return vm;
        }
    }
}